package com.etc.zzsys.dao.impl;

import java.util.List;

import com.etc.zzsys.dao.UsersDao;
import com.etc.zzsys.entiy.Users;
import com.etc.zzsys.util.BaseDao;

/**
 * 自定义UsersDao
 * 
 * @author knowno
 *
 */
public class UsersDaoImpl implements UsersDao {

	/**
	 * 查询所有的用户信息
	 * 
	 * @return List<Users>
	 */
	@Override
	public List<Users> queryUsers() {
		@SuppressWarnings("unchecked")
		List<Users> list = (List<Users>) BaseDao.select("select  * from users", Users.class);
		return list;
	}

	/**
	 * 添加 用户的方法
	 * 
	 * @param u
	 *            users对象
	 * @return true 操作成功 FALSE 操作失败
	 */
	@Override
	public boolean addUsers(Users u) {
		// TODO Auto-generated method stub
		// userState 为1表示启用，这里直接赋值就好了
		String sql = "INSERT INTO users(userName, userPwd, userSex, userTel, userAddress, userLevel,userState) VALUES (?, ?, ?, ?, ?, ?,1)";
		return BaseDao.execute(sql, u.getUserName(), u.getUserPwd(), u.getUserSex(), u.getUserTel(), u.getUserAddress(),
				u.getUserLevel()) > 0;
	}

	@Override
	public boolean delUsers(int userId) {
		// TODO Auto-generated method stub
		String sql = "delete from users where userId=?";
		return BaseDao.execute(sql, userId) > 0;
	}

	@Override
	public boolean updateUsers(Users u) {
		// TODO Auto-generated method stub
		String sql = "update users set userName=?,userSex=?,userTel=?,userAddress=?,userLevel=?,userState=? where userId=?";
		return BaseDao.execute(sql, u.getUserName(), u.getUserSex(), u.getUserTel(), u.getUserAddress(),
				u.getUserLevel(), u.getUserState(), u.getUserId()) > 0;

	}

	/**
	 * 根据用户名和密码来做查询
	 */
	@Override
	public Users queryByNameAndPwd(String userName, String userPwd) {
		// TODO Auto-generated method stub
		// 我们的密码是有加密处理
		String sql = "select  * from users where userName=? and userPwd=?";
		List<Users> list = (List<Users>) BaseDao.select(sql, Users.class, userName, userPwd);

		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
